package com.jxl.springboot.game.controller;

import com.jxl.springboot.game.pojo.GameInfo;
import com.jxl.springboot.game.pojo.GameType;
import com.jxl.springboot.game.pojo.Page;
import com.jxl.springboot.game.service.GameInfoService;
import com.jxl.springboot.game.service.GameTypeService;
import com.jxl.springboot.game.utils.CookieUtils;
import com.jxl.springboot.game.web.WebResult;
import com.jxl.springboot.game.web.constant.Constant;
import com.jxl.springboot.game.web.constant.GameInfoConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 游戏
 * @author 江雪林
 * @create 2021-04-24
 */
@Controller
@RequestMapping("/games")
public class GameController {
    @Autowired
    private GameTypeService gameTypeService;
    @Autowired
    private GameInfoService gameInfoService;

    @RequestMapping("/type")
    @ResponseBody
    public WebResult gameType(){
        WebResult result = new WebResult(Constant.SUCCESS,Constant.OK);
        List<GameType> games = gameTypeService.getAll();
        result.put(Constant.GAME_TYPE,games);
        return result;
    }

    @InitBinder
    public void dateBinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping(value = "/game/{pageNo}/{pageSize}", method = RequestMethod.GET)
    public ModelAndView getPassedGame(@PathVariable("pageNo") Integer pageNo,
                                      @PathVariable(value = "pageSize", required = false) Integer pageSize) {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 5;
        }
        ModelAndView modelAndView = new ModelAndView("games");
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageOfPassedGame(pageNo, pageSize);
        modelAndView.addObject("page", pageOfPassedGame);
        return modelAndView;
    }

    @RequestMapping(value = "/ugame/{id}/{pageNo}/{pageSize}", method = RequestMethod.GET)
    public ModelAndView getGamesByUser(@PathVariable(value = "id") Integer id,
                                       @PathVariable("pageNo") Integer pageNo,
                                       @PathVariable(value = "pageSize", required = false) Integer pageSize) {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 5;
        }
        ModelAndView modelAndView = new ModelAndView("usergames");
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(id, pageNo, pageSize);
        modelAndView.addObject("page", pageOfPassedGame);
        return modelAndView;
    }

    @RequestMapping(value = "/game/{id}", method = RequestMethod.GET)
    public ModelAndView getGameInfo(@PathVariable(value = "id") Integer id) {
        if (id <= 0) {
            return null;
        }
        ModelAndView modelAndView = new ModelAndView("update");
        modelAndView.addObject("game", gameInfoService.getGameInfoById(id));
        return modelAndView;
    }

    @RequestMapping(value = "/game/update", method = RequestMethod.POST)
    public ModelAndView update(GameInfo gameInfo, HttpServletRequest req) {
        ModelAndView modelAndView = new ModelAndView();
        if (gameInfo.getId() <= 0 || gameInfo == null) {
            return null;
        }
        gameInfo.setStatus(GameInfoConstant.WAIT_PASSED);
        if (gameInfoService.udpateGame(gameInfo)) {
            modelAndView.setViewName("usergames");
            //获取当前用户登录id
            int uid = Integer.parseInt(CookieUtils.getCookieValueByName(req,Constant.U_ID).replace(Constant.U_ID,""));
            Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(uid, 1, 5);
            modelAndView.addObject("page", pageOfPassedGame);
        } else {
            modelAndView.setViewName("update");
            modelAndView.addObject("game", gameInfoService.getGameInfoById(gameInfo.getId()));
        }
        return modelAndView;
    }

    @RequestMapping(value = "/game/status/{id}", method = RequestMethod.GET)
    public ModelAndView updateStatus(@PathVariable("id") Integer id, HttpServletRequest req) {
        ModelAndView modelAndView = new ModelAndView();
        if (id == null || id <= 0) {
            return null;
        }
        gameInfoService.udpateGame(new GameInfo(id, GameInfoConstant.PASSING));
        modelAndView.setViewName("usergames");
        //获取当前用户登录id
        int uid = Integer.parseInt(CookieUtils.getCookieValueByName(req,Constant.U_ID).replace(Constant.U_ID,""));
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(uid, 1, 5);
        modelAndView.addObject("page", pageOfPassedGame);
        return modelAndView;
    }
}
